//删除提示
function delBtn(_url, obj) {
    _id = $(obj).attr('data-id');
    layer.confirm(
        "确定要删除此项?",
        {skin: 'layui-layer-molv', icon: 3, title: "温馨提示", shadeClose: true},
        function (index) {
            ajaxdel(_url, {id: _id}, index)
            layer.close(index);
        })
}
//批量删除
function multidel(_url, _num, _data) {
    layer.confirm(
        "确定要删除选中的" + _num + "项?",
        {skin: 'layui-layer-molv', icon: 3, title: "温馨提示", shadeClose: true},
        function (index) {
            ajaxdel(_url, _data, index)
        })
}
//ajax请求错误提示
function ajaxerror(err) {
    layer.alert(err, {
        skin: 'layui-layer-lan' //样式类名
        , closeBtn: 0
    });
}
//删除请求
function ajaxdel(_url, _data, _index,_type) {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        url: _url,
        type: 'post',
        dataType: 'json',
        data: _data,
        success: function (json) {
            if (json.status == 1) {
                layer.close(_index);
                window.location.reload();
            }
        },
        error: function () {
            ajaxerror('网络似乎出了点问题....');
        }
    });
}
$(document).ready(function () {
    //全选的实现
    $('.check-all').on('ifChecked ifUnchecked', function (event) {
        if (event.type == 'ifChecked') {
            $('input[name="ids[]"]').iCheck('check');
        } else {
            $('input[name="ids[]"]').iCheck('uncheck');
        }
    });
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });
    var checkAll = $('.check-all');  //全选的input
    var checkboxs = $('input[name="ids[]"]'); //所有单选的input
    checkboxs.on('ifChanged', function (event) {
        if (checkboxs.filter(':checked').length == checkboxs.length) {
            checkAll.prop('checked', true);
        } else {
            checkAll.prop('checked', false);
        }
        checkAll.iCheck('update');
    })
    //获取选中的checkbox值
    $('.btn-del').on('click', function () {
        var checkedboxs = checkboxs.filter(':checked')
        if (checkedboxs.length == 0) {
            layer.msg('请选择要删除的项');
            return;
        } else {
            var checkvalue = [];
            $.each(checkedboxs, function (i, v) {
                checkvalue.push($(v).val());
            })
            multidel(_url, checkvalue.length, {id: checkvalue})

        }
    });

});
